package com.tarena.lbs.marketing.web.controller;

import com.alibaba.fastjson2.JSONObject;
import com.tarena.lbs.marketing.web.service.CouponService;
import com.tarena.lbs.marketing.web.service.UserReceiveCouponService;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.marketing.converter.UserReceiveCouponConverter;
import com.tarena.lbs.pojo.marketing.bo.UserReceiveCouponBO;
import com.tarena.lbs.pojo.marketing.param.UserReceiveCouponParam;
import com.tarena.lbs.pojo.marketing.query.UserCouponCodeQuery;
import com.tarena.lbs.pojo.marketing.query.UserCouponQuery;
import com.tarena.lbs.pojo.marketing.vo.UserReceiveCouponVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("/marketing/user/receive")
@Api(tags = "用户领取优惠券管理")
@Slf4j
public class UserReceiveCouponController {

    @Resource
    private UserReceiveCouponService userReceiveCouponService;

    @Resource
    private CouponService couponService;

    @Resource
    private UserReceiveCouponConverter userReceiveCouponConverter;

    @PostMapping("/save")
    @ApiOperation("用户领取优惠券")
    public Result<Void> userReceiveCoupon(@Valid @RequestBody UserReceiveCouponParam param) throws Exception {
        log.info("UserReceiveCouponController userReceiveCoupon param={}", JSONObject.toJSONString(param));
        couponService.executeAsync(param);
        return Result.success();
    }

    @PostMapping("/list")
    @ApiOperation("用户优惠券列表")
    public Result<PageResult<UserReceiveCouponVO>> getUserCoupon(@Valid @RequestBody UserCouponQuery query) {
        BasePageList<UserReceiveCouponBO> couponBos = userReceiveCouponService.userList(query);
        PageResult<UserReceiveCouponVO> page = userReceiveCouponConverter.assemblePageVOS(couponBos,query);
        return new Result<>(page);
    }

    @PostMapping("/detail")
    @ApiOperation("优惠券领取详情")
    public Result<UserReceiveCouponVO> userCouponDetail(@Valid @RequestBody UserCouponCodeQuery query) {
        UserReceiveCouponBO userReceiveCouponBO = userReceiveCouponService.userCouponDetail(query);
        return new Result<>(userReceiveCouponConverter.bo2vo(userReceiveCouponBO));
    }

}
